package com.m3839.sdk.login.ui;

import android.app.Fragment;
import android.content.ComponentName;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;

import com.m3839.sdk.common.core.Constant;
import com.m3839.sdk.common.core.ErrorConstant;
import com.m3839.sdk.common.core.SdkConfiguration;
import com.m3839.sdk.common.encrypt.Base64Utils;
import com.m3839.sdk.common.encrypt.Md5Utils;
import com.m3839.sdk.common.util.AppUtils;
import com.m3839.sdk.common.util.LogUtils;
import com.m3839.sdk.login.core.LoginManager;
import com.m3839.sdk.login.bean.GameConfig;
import com.m3839.sdk.login.core.InitManager;

/**
 * Description:登录调用或者账号切换
 *
 * @author 陈宝阳
 * @create 2020/8/3 18:28
 */
public class LoginHostFragment extends Fragment {

  private static final String TAG = "login";

  /**
   * 用户登陆校验的请求码
   */
  private static final int REQUEST_CODE_USER = 1001;

  /**
   * 签名固定前缀，明文：SFlLQitMT0dJTg, base64后  U0ZsTFFpdE1UMGRKVGcK
   */
  private static final String SIGN_PREV_SALTS = "U0ZsTFFpdE1UMGRKVGcK";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getUserInfoFromHykbApp();
  }

  public void getUserInfoFromHykbApp() {
    GameConfig initConfig = InitManager.getInstance().getInitConfig();
    String appId = SdkConfiguration.getGameId();
    if (initConfig == null || TextUtils.isEmpty(appId)) {
      LogUtils.e(TAG, "SDK未进行初始化或者SDK初始化配置信息获取失败！");
      return;
    }

    Intent intent = new Intent();
    ComponentName componentName = new ComponentName(Constant.HYKB_PACKAGE_NAME,
            Constant.HYKB_LOGIN_ACTIVITY);
    intent.setComponent(componentName);
    intent.putExtra(Constant.HYKB_KEY_APP_ID, appId);
    intent.putExtra(Constant.HYKB_KEY_GAME_NAME, initConfig.getGameName());
    intent.putExtra(Constant.HYKB_KEY_GAME_ICON, initConfig.getGameIcon());
    String sha1 = AppUtils.getSignInfo(getActivity());
    LogUtils.i("m3839", sha1);
    String sign = Md5Utils.md5(Base64Utils.decode(SIGN_PREV_SALTS)  + sha1);
    intent.putExtra(Constant.HYKB_KEY_SIGN_INFO, sign);
    // 调用快爆登陆验证的固定标识
    intent.putExtra(Constant.HYKB_KEY_USER_INFO_SDK_QUEST,
            Constant.HYKB_VALUE_USER_INFO_SDK_QUEST);

    startActivityForResult(intent, REQUEST_CODE_USER);
  }

  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    //你的代码
    super.onConfigurationChanged(newConfig);
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_USER) {
      if (resultCode == Constant.HYKB_CODE_LOGIN_SUCCESS && data != null) {
        //用户授权校验成功后的数据返回
        String uid = data.getStringExtra(Constant.HYKB_KEY_USER_ID);
        String token = data.getStringExtra(Constant.HYKB_KEY_USER_TOKEN);
        String type = data.getStringExtra(Constant.HYKB_KEY_USER_LOGIN_TYPE);
        String nick = data.getStringExtra(Constant.HYKB_KEY_USER_NICK);

        if (TextUtils.isEmpty(uid)
            || TextUtils.isEmpty(type)
            || TextUtils.isEmpty(token)
            || TextUtils.isEmpty(nick)) {
          //数据获取异常
          LoginManager.getInstance()
              .handleErrorCallback(ErrorConstant.HYKB_CODE_LOGIN_DATA_EXCEPTION,
                      ErrorConstant.ERR_MSG_SDK_LOGIN_DATA_EXCEPTION);
        } else {
          LoginManager.getInstance().createUser(uid, nick, type, token);
        }
      } else if (resultCode == ErrorConstant.HYKB_CODE_LOGIN_FAIL && data != null) {
        //获取用户信息失败的返回
        String msg = data.getStringExtra(Constant.HYKB_KEY_USER_FAILING_MSG);
        LoginManager.getInstance().handleErrorCallback(resultCode, msg);
      } else {
        //这边取消有两个情况：一是有登录的情况，二是没有登录的情况下
        LoginManager.getInstance().handleErrorCallback(ErrorConstant.HYKB_CODE_LOGIN_CANCEL,
                ErrorConstant.ERR_MSG_SDK_LOGIN_CANCEL);
      }
    }
  }
}
